package com.mojie.overscroll.demo.recyclerview

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * @Author:         lxp
 * @CreateDate:     2024/8/9
 */
class SpaceItemDecoration(
    private val spaceHorizontal: Int,
    private val spaceVertical: Int
) : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val layoutManager = parent.layoutManager
        if (layoutManager is LinearLayoutManager) {
            val position = parent.getChildAdapterPosition(view)
            if (layoutManager.orientation == LinearLayoutManager.VERTICAL) {
                if (position == 0) {
                    outRect.top = spaceVertical
                }
                outRect.bottom = spaceVertical
            } else {
                if (position == 0) {
                    outRect.left = spaceHorizontal
                }
                outRect.right = spaceHorizontal
            }
        }
    }
}